﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LineWord : MonoBehaviour {

    public string answer;
    public float cellSize;
    public List<Cell> cells = new List<Cell>();
    public int numLetters;
    public float lineWidth, lineHeight;
    public CrossedWord crossedWord;

    public bool isShown;

    public void Build()
    {
        numLetters = answer.Length;
        float cellGap = cellSize * Const.CELL_GAP_COEF;

        for(int i = 0; i < numLetters; i++)
        {
            int xDelta = crossedWord.horizontal ? i : 0;
            int yDelta = crossedWord.horizontal ? 0 : -i;
            Position position = new Position() { x = crossedWord.position.x + xDelta, y = crossedWord.position.y + yDelta };

            Cell cell = Instantiate(MonoUtils.instance.cell);
            cell.letter = answer[i].ToString();
            cell.letterText.transform.GetChild(0).localScale = Vector3.one * (cellSize / 100f);
            RectTransform cellTransform = cell.GetComponent<RectTransform>();
            cellTransform.SetParent(transform);
            cellTransform.sizeDelta = new Vector2(cellSize, cellSize);
            cellTransform.localScale = Vector3.one;
            cell.position = position;

            float x, y;

            if (crossedWord.horizontal)
            {
                x = i * (cellSize + cellGap);
                y = 0;
            }
            else
            {
                x = 0;
                y = -i * (cellSize + cellGap);
            }

            cellTransform.localPosition = new Vector3(x, y);
            cells.Add(cell);
        }

        foreach(var cell in cells)
        {
            if (GameState.mainCells.ContainsKey(cell.position))
            {
                cell.gameObject.SetActive(false);
            }
            else
            {
                GameState.mainCells.Add(cell.position, cell);
            }
        }
    }

    public void SetLineWidth()
    {
        int numLetters = answer.Length;
        var rt = GetComponent<RectTransform>();

        var length = numLetters * cellSize + (numLetters - 1) * cellSize * Const.CELL_GAP_COEF;
        if (crossedWord.horizontal)
        {
            lineWidth = length;
            lineHeight = cellSize;
        }
        else
        {
            lineWidth = cellSize;
            lineHeight = length;
        }
        rt.sizeDelta = new Vector2(lineWidth, lineHeight);
    }

    public void SetProgress(string progress)
    {
        isShown = true;
        int i = 0;
        foreach(var cell in cells)
        {
            if (progress[i] == '1')
            {
                cell.IsShown = true;
                cell.ShowText();
            }
            else
            {
                isShown = false;
            }
            i++;
        }
    }

    public void ShowAnswer()
    {
        isShown = true;

        foreach (var cell in cells)
        {
            cell.ShowAnswer();
        }
    }

    public void ShowHint()
    {
        for(int i = 0; i < cells.Count; i++)
        {
            var cell = cells[i];
            if (!cell.IsShown)
            {
                cell.ShowAnswer();
                if (IsAllShown())
                {
                    isShown = true;
                }
                return;
            }
        }
    }

    public bool IsAllShown()
    {
        foreach (var cell in cells)
        {
            if (!cell.IsShown) return false;
        }
        return true;
    }
}
